//获取购物车列表数据的方法写在onShow
//获取应用实例
const app = getApp()
var numbers = 1;
var bool = true;
Page({
  data: {
    CustomBar: app.globalData.CustomBar,
    scanFunctionIsUserAble: true, //控制当前扫码是否可用
    tempcode: 123456789, //变量保存code
    currentId: '1',
    tabSection: [{
        name: '扫码录入',
        typeId: '1'
      },
      {
        name: '选择商品',
        typeId: '2'
      }
    ],
    classList: [{
        name: 'all',
        value: '全部商品'
      },
      {
        name: 'water',
        value: '飲料酒水',
      },
      {
        name: 'BRA',
        value: '糖果餅乾'
      },
      {
        name: 'JPN',
        value: '粮油副食'
      },
      {
        name: 'ENG',
        value: '数码家电'
      },
      {
        name: 'FRA',
        value: '医药保健'
      },
    ],
    sortList: [{
        name: 'price-up',
        value: '按价格-升序'
      },
      {
        name: 'price-down',
        value: '按价格-降序'
      },
      {
        name: 'storage-up',
        value: '按库存量-升序'
      },
      {
        name: 'storage-down',
        value: '按库存量-降序'
      },
    ],
    // list: [],               // 购物车列表
    // hasList: false,          // 列表是否有数据
    // 默认展示数据
    // 商品列表数据
    list: [{
        id: 1,
        name: '园艺大师抗皱精华露',
        image: '../../../../../image/timg.jpg',
        pro_name: "30ml",
        num: 1,
        retailPrice: 180,
        selected: false
      },
      {
        id: 2,
        name: '伊芙琳玫瑰护手霜',
        image: '../../../../../image/timg.jpg',
        pro_name: "25g",
        num: 1,
        retailPrice: 62,
        selected: false
      },
      {
        id: 2,
        name: '燕麦山羊乳舒缓护手霜',
        image: '../../../../../image/timg.jpg',
        pro_name: "75ml",
        num: 1,
        retailPrice: 175,
        selected: false
      }
    ],
    //扫码得到的商品数组
    scanList: [{
     id: 2,
      name: '燕麦山羊乳舒缓护手霜',
      image: '../../../../../image/timg.jpg',
      pro_name: "75ml",
      num: 1,
      retailPrice: 175,
      selected: true
    }],
    // 金额
    totalPrice: 0, // 总价，初始为0
    totalClass: 0,
    totalNum: 0,
    // 全选状态
    selectAllStatus: false, // 全选状态
  },
  onLoad:function(){
    var that = this;
    var classList;
    wx.request({
      url: "http://47.106.145.141:8080/api/v1/type",
      method: "GET",
      data: {
        "token": "F8170031DB0BA03CDBC627822454E8FA",
        "shopId": "832824",
      },
      success: function (result) {
        that.setData({
          classList: classList = result.data.dataInfo,
        })
      }
    })

    wx.request({
      url: "http://47.106.145.141:8080/api/v1/goods",
      method: "GET",
      data: {
        "token": "F8170031DB0BA03CDBC627822454E8FA",
        "shopId": "832824",
        "pageNum": 1,
        "pageSize": 300,
      },
      success: function (result) {
        that.setData({
          goodList:result.data.dataInfo,
        })

        console.log(that.data.goodList);
      }
    })

    

    

  },

  takeCode(e) {
    var fileCode = e.detail.result; //临时变量保存code
    var that = this;
    var scanList = that.data.scanList;
    if (this.data.scanFunctionIsUserAble) { //判断当前是否可用scan
      this.setData({
        scanFunctionIsUserAble: false // 设置当前不可用scan 被占用
      })
      console.log("1.开始扫码了: "); //进入扫码阶段
      if (this.data.tempcode === fileCode) { //若条形码重复则放弃当前scan使用权
        console.log("2.条形码重复");
        this.setData({
          scanFunctionIsUserAble: true
        });

      } else { //若不重复则现实code的内容并且调用弹窗，完成后放弃当前scan使用权
        console.log("2." + fileCode);
        var obj = {
          code: fileCode,
          name: "临时名字",
          num: 1, //默认1,
          selected: true,
          retailPrice: 62,
        }
        scanList.unshift(obj)
        this.setData({
          tempcode: fileCode, //存储code
          scanList: scanList
        });
        this.showModal(); //调用拟态框显示        
        console.log("3.临时code为" + this.data.tempcode);
        this.setData({
          scanFunctionIsUserAble: true //放弃当前scan使用权
        });
      }
    }
  },

  //点击每个导航的点击事件
  tabSelect: function(e) {
    let id = e.currentTarget.id;
    if (id) {
      this.setData({
        currentId: id
      })
    }
  },

  onShow() {
    wx.showToast({
      title: '加载中',
      icon: "loading",
      duration: 1000
    })

    // 价格方法
    this.count_price();
    this.count_num();
  },
  /**
   * 当前商品选中事件
   */
  //5.4-14:43打个标记 待会要把选中商品添加进scanList
  selectList(e) {
    var that = this;
    // 获取选中的radio索引
    var index = e.currentTarget.dataset.index;
    // 获取到商品列表数据
    var list = that.data.list;
    var scanList = that.data.scanList;
    // 默认全选
    that.data.selectAllStatus = true;
    // 循环数组数据，判断----选中/未选中[selected]
    list[index].selected = !list[index].selected;
    // 如果数组数据全部为selected[true],全选
    for (var i = list.length - 1; i >= 0; i--) {
      if (!list[i].selected) {
        that.data.selectAllStatus = false;
        break;
      }
      // 重新渲染数据
      that.setData({
        list: list,
        //scanList: scanList,
        selectAllStatus: that.data.selectAllStatus
      })
      // 调用计算金额方法
      that.count_price();
      that.count_num();
    }
    //把选中商品添加进scanList
    for (var i = list.length - 1; i >= 0; i--) {
      if (list[i].selected) {
        scanList.unshift(list[i]);
      }
      else{
        scanList.splice(i,1)
      }
      that.setData({
        //list: list,
        scanList: scanList,
        selectAllStatus: that.data.selectAllStatus
      })
      // 调用计算金额方法
      that.count_price();
      that.count_num();
    }
  },




  /**
   * 购物车全选事件
   */
  selectAll(e) {
    // 全选ICON默认选中
    let selectAllStatus = this.data.selectAllStatus;
    // true  -----   false
    selectAllStatus = !selectAllStatus;
    // 获取商品数据
    let list = this.data.list;
    // 循环遍历判断列表中的数据是否选中
    for (let i = 0; i < list.length; i++) {
      list[i].selected = selectAllStatus;
    }
    // 页面重新渲染
    this.setData({
      selectAllStatus: selectAllStatus,
      list: list
    });
    // 计算金额方法
    this.count_price();
    this.count_num();
  },

  /**
   * 绑定加数量事件
   */
  btn_add(e) {
    // 获取点击的索引
    const index = e.currentTarget.dataset.index;
    // 获取商品数据
    let list = this.data.list;
    // 获取商品数量
    let num = list[index].num;
    // 点击递增
    num = num + 1;
    list[index].num = num;
    // 重新渲染 ---显示新的数量
    this.setData({
      list: list
    });
    // 计算金额方法
    this.count_price();
    this.count_num();
  },

  /**
   * 绑定减数量事件
   */
  btn_minus(e) {
    //   // 获取点击的索引
    const index = e.currentTarget.dataset.index;
    // const obj = e.currentTarget.dataset.obj;
    // console.log(obj);
    // 获取商品数据
    let list = this.data.list;
    // 获取商品数量
    let num = list[index].num;
    // 判断num小于等于1  return; 点击无效
    if (num <= 1) {
      return false;
    }
    // else  num大于1  点击减按钮  数量--
    num = num - 1;
    list[index].num = num;
    // 渲染页面
    this.setData({
      list: list
    });
    // 调用计算金额方法
    this.count_price();
    this.count_num();
  },
  // 提交订单
  btn_submit_order: function() {
    var that = this;
    console.log(that.data.totalPrice);

    wx.showModal({
      title: '提示',
      content: '合计金额-' + that.data.totalPrice + "暂未开发",
    })
  },

  /**
   * 计算总价
   */
  count_price() {
    // 获取商品列表数据
    let list = this.data.scanList;
    // 声明一个变量接收数组列表price
    let total = 0;
    // 循环列表得到每个数据
    for (let i = 0; i < list.length; i++) {
      // 判断选中计算价格
      if (list[i].selected) {
        // 所有价格加起来 count_money
        total += list[i].num * list[i].retailPrice;
      }
    }
    // 最后赋值到data中渲染到页面
    this.setData({
      scanList: list,
      totalPrice: total.toFixed(2)
    });
  },
  count_num: function(e) {
    let list = this.data.scanList;
    let total = 0;
    for (let i = 0; i < list.length; i++) {
      if (list[i].selected) {
        total += list[i].num;
      }
    }
    this.setData({
      scanList: list,
      totalNum: total
    });
  },
  show2Modal(e) {
    if (e.currentTarget.dataset.target === "classModal") {
      this.setData({
        cicon: "icon-fold"
      })
    }
    if (e.currentTarget.dataset.target === "sortModal") {
      this.setData({
        sicon: "icon-fold",
      })
    }
    this.setData({
      //modalName: "display",
      modalName: e.currentTarget.dataset.target,

    })
  },
  //选择分类后返回相应的商品
  classChange: function(e) {
    console.log('用户选择了分类', e.detail.value);
    this.setData({
      superMan: e.detail.value,
      modalName:null
    })
  },
  //前台将本地商品数组进行排序
  sortChange: function(e) {

  },
  showModal(e) {
    this.setData({
      modalName: "display",
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      sicon: "icon-unfold",
      cicon: "icon-unfold"
    })
  },
  //点击进货按钮
  addStorage: function(e) {
    wx.navigateTo({
      url: '../create-storage/create-storage',
    })
  }
})